<?php

namespace app\personal\controller;

use think\Controller;
use think\Request;
use think\Db;

class Message extends Controller
{
    /**
     *   初始化
     *  判断是否登陆
     */
    public function _initialize()
    {
        if (session('tel') == null){
            return $this->error('您还未登陆,请先登陆!');
        }
    }

    /**
     *
     * 显示个人信息列表
     *
     */
    public function index()
    {
        $message = Db::table(['user_func', 'user_self'])
            ->field('user_func.tel,user_self.nickname,user_self.email,user_self.sex,user_self.icon,user_func.uid')
            ->where('user_func.uid=user_self.uid and user_func.uid='.session('uid'))
            ->find();
        $ass = Db::name('subscribe s')
            ->join('column c','s.cid = c.cid')
            ->where('s.uid ='.session('uid'))
            ->limit(9)
            ->select();
        $user = Db::table('user_self')->field('nickname,icon')->where('uid ='.session('uid'))->find();
//        dump($message);die;
        return view('Message/index', [
            'title' => '【众评网】为你私人定制的资讯客户端 --- 【che-cn.com】 - 个人中心',
            'message' => $message,
            'ass' => $ass,
            'user' => $user,
        ]);
    }

    /**
     *
     * 修改个人资料的资源
     *
     * @param  \think\Request  $request
     *
     * @return \think\Response
     */
    public function update()
    {
        $data = request()->post();
//        dump($data);die;
        $rst = $this->validate(
            [
                'nickname'=>$data['nickname'],
                'email'=>$data['email'],
                'sex'=>$data['sex'],
                'icon'=>$data['icon'],
            ],
            [
                ['nickname','require|chsDash|length:3,6','内容不能为空!|只能是汉字、字母、数字和下划线_|只能输入3 ~ 6个字符'],
                ['email','require|email','邮箱不能为空! | 邮箱格式不正确'],
                ['sex','require|in:0,1','格式不正确!'],
            ]);
        if(true !== $rst){
            // 验证失败 输出错误信息
            return $this->error($rst);
        }
        $info =[
            'nickname'=>$data['nickname'],
            'email'=>$data['email'],
            'sex'=>$data['sex'],
            'icon'=>$data['icon'],
        ];
        $res =Db::table('user_self')->where('uid='.session('uid'))->update($info);
        if ($res > 0){
            return $this->success('修改成功',url('/personal'));
        } else {
            return $this->success('修改失败');
        }
    }

    /**
     * 修改密码
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
           $data = request()->post();
           if (!isset($data['nump'])){
                return $this->success('验证码不正确~~');
           }
        if (!$data['nump'] == session('yzm')){
            return $this->success('验证码不正确');
        }
//           判断密码

        $rst = $this->validate(
            [
                'newpwd'=>$data['newpwd'],
                'newpwd'=>$data['repwd'],
            ],
            [
                ['newpwd','^[a-zA-Z]\w{5,17}$','以字母开头，长度在6~18之间，只能包含字母、数字和下划线'],
                ['repwd','^[a-zA-Z]\w{5,17}$','以字母开头，长度在6~18之间，只能包含字母、数字和下划线'],
            ]);
        if(true !== $rst){
            // 验证失败 输出错误信息
            return $this->error($rst);
        }


            $res = Db::table('user_func')->field('pwd')->where('uid ='.session('uid'))->find();
           if ($res !== md5($data['pwd'])){
               return $this->success('原密码不正确');
           }
           if ($data['newpwd'] !== $data['repwd']){
               return $this->success('两次新密码不相同');
           }
           $info = [
                'pwd'=> md5($data['repwd']),
           ];
           $result = Db::table('user_func')->where('uid ='.session('uid'))->update($info);
           if ($result > 0){
               return $this->success('修改成功','/personal');
           } else {
               return $this->error('修改失败');
           }
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 发送手机签证码.
     *
     * @param  int  $id 手机号
     * @return \think\Response
     */
    public function send($id)
    {
        /**
         * 发送模板短信
         * @param to 手机号码集合,用英文逗号分开
         * @param datas 内容数据 格式为数组 例如：array('Marry','Alon')，如不需替换请填 null
         * @param $tempId 模板Id
         */
        function sendTemplateSMS($to,$datas,$tempId)
        {

            //主帐号
            $accountSid= '8a216da85cce7c54015ce7c3b8b8084f';
            //主帐号Token
            $accountToken= '0f324c2bf9fa4463a2dc0cb03bc0d404';
            //应用Id
            $appId='8a216da85cce7c54015ce7c3b9050855';
            //请求地址，格式如下，不需要写https://
//            $serverIP='sandboxapp.cloopen.com';
            $serverIP='app.cloopen.com';
            //请求端口
            $serverPort='8883';
            //REST版本号
            $softVersion='2013-12-26';
//            // 初始化REST SDK
//            global $accountSid,$accountToken,$appId,$serverIP,$serverPort,$softVersion;
            $rest = new CCPRestSDK($serverIP,$serverPort,$softVersion);
            $rest->setAccount($accountSid,$accountToken);
            $rest->setAppId($appId);
            // 发送模板短信
            echo "Sending TemplateSMS to $to <br/>";
            $result = $rest->sendTemplateSMS($to,$datas,$tempId);
            if($result == NULL ) {
                echo "result error!";
            }
            if($result->statusCode!=0) {
                echo "error code :" . $result->statusCode . "<br>";
                echo "error msg :" . $result->statusMsg . "<br>";
            }else{
                echo "Sendind TemplateSMS success!<br/>";
                // 获取返回信息
                $smsmessage = $result->TemplateSMS;
                echo "dateCreated:".$smsmessage->dateCreated."<br/>";
                echo "smsMessageSid:".$smsmessage->smsMessageSid."<br/>";
            }
        }
        $rand = rand(100000,199999);
        session('yzm',$rand);
//        dump(array($rand,'1'));die;
        //Demo调用,参数填入正确后，放开注释可以调用
<<<<<<< HEAD
       sendTemplateSMS($id,array($rand,'1'),"1");
=======
//        sendTemplateSMS($id,array($rand,'1'),"1");
>>>>>>> 2d79cfa8789da2f2a423d006d49f501a7ccfc9c7

        return json($rand);
    }



    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
